package com.gopay.common.cps.dao.cbrmb.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cps.dao.cbrmb.CbRmbCarrierInfoDAO;
import com.gopay.common.domain.cbrmb.CbRmbCarrierInfo;

/**
 * 跨境人民币  承运方信息表DAO实现类
 * 
 * @CbRmbCarrierInfoDAOImpl.java
 * @author lailingyu
 * @2016-12-7 下午6:7:52 www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("cbRmbCarrierInfoDAO")
public class CbRmbCarrierInfoDAOImpl extends BaseDAOImpl<CbRmbCarrierInfo, Integer> implements CbRmbCarrierInfoDAO{

    /**
     * 通过承运方名称查询承运方代码
     * 
     * @param name
     * @return code
     */
    @Override
    public Integer findCodeByName(String name) {
        String hql = "from CbRmbCarrierInfo where name =:name";
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("name", name);
        List<CbRmbCarrierInfo> list = findByHql(hql, param);
        if (list != null && list.size() > 0) {
            return list.get(0).getCode();
        } else {
            return null;
        }
    }

    /**
     * 通过承运方代码查询承运方名称
     * 
     * @param code
     * @return name
     */
    @Override
    public String findNameByCode(Integer code) {
        String hql = "from CbRmbCarrierInfo where code =:code";
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("code", code);
        List<CbRmbCarrierInfo> list = findByHql(hql, param);
        if (list != null && list.size() > 0) {
            return list.get(0).getName();
        } else {
            return null;
        }
    }

    /**
     * 保存客户自定义承运方信息
     * 
     * @param name
     * @param custId
     * @return code
     */
    @Override
    public Integer saveCarrier(String name, String custId) {
        CbRmbCarrierInfo cbRmbCarrierInfo = new CbRmbCarrierInfo();
        cbRmbCarrierInfo.setName(name);
        cbRmbCarrierInfo.setCustId(custId);
        Date date = new Date();
        cbRmbCarrierInfo.setCreatedTs(date);
        cbRmbCarrierInfo.setLastUpdTs(date);
        cbRmbCarrierInfo = save(cbRmbCarrierInfo);

        return cbRmbCarrierInfo.getCode();
    }

    @Override
    public List<CbRmbCarrierInfo> findByCustIdAndName(String custId, String name) {
        String hql = "from CbRmbCarrierInfo where name=:carrierName and custId in ('0',:cust_id) order by custId";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("carrierName", name);
        paramMap.put("cust_id", custId);
        return super.findByHql(hql, paramMap);
    }
    
}
